package com.c.ctgproject.http

import com.c.ctgproject.base.BaseView
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 *作者:LiBW
 *创建日期: 2020/8/15
 *描述:网络请求回调处理 默认处理错误
 */
open class MvpObserver(private val listener: ObserverListener, private val mView: BaseView?, private var showLoading: Boolean = true) : Observer<ResponseBody> {
    //加载完成 进度条消失
    override fun onComplete() {
        mView?.hideLoading()
    }

    //加载开始 展示进度条
    override fun onSubscribe(d: Disposable) {
        if (showLoading) {
            mView?.showLoading()
        }
    }

    //加载完成回调
    override fun onNext(t: ResponseBody) {
        mView?.hideLoading()
        listener.onNext(t)
    }

    //出现错误 进度条消失 按需求展示错误页面
    override fun onError(e: Throwable) {
        mView?.hideLoading()
        mView?.onError(e.message.toString())
        listener.onError(e.message.toString())
    }
}

interface ObserverListener {
    fun onNext(t: ResponseBody)
    fun onError(error: String)
}


